function t(t, e, a) {
    return e in t ? Object.defineProperty(t, e, {
        value: a,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : t[e] = a, t;
}

function e(t, a) {
    s.showLoading("正在提交数据");
    var i = s.getUTCDateInSeconds(t.data.date), n = s.getTimeInSeconds(t.data.time), d = a.detail.value.token || "", r = a.detail.value.tokenDesc || "", l = t.data.rule, u = t.data.title;
    "mini" == t.data.lottoType && (l = a.detail.value.rule, u = t.data.prizes[0].name);
    var h = {
        lid: t.data.lid,
        title: u,
        mode: t.data.mode,
        content: l,
        type: parseInt(t.data.typeIndex),
        prizes: t.data.prizes,
        banners: t.data.banners,
        sign_name: t.data.signName,
        wx_no: t.data.wxNo,
        no_forward: 1 == t.data.forwardIndex ? 1 : 0,
        group_qr: t.data.qrcode || "",
        req_token: d.trim(),
        token_desc: r.trim(),
        access_token: wx.getStorageSync("accessToken")
    };
    if (0 == t.data.typeIndex ? h.date = i + n : 1 == t.data.typeIndex ? h.limit = parseInt(t.data.joinNum) : 2 == t.data.typeIndex && (h.limit = 0), 
    2 == t.data.mode) {
        h.content = a.detail.value.rule, h.start_date = s.getUTCDateInSeconds(t.data.dateStart) + s.getTimeInSeconds(t.data.timeStart), 
        h.end_date = s.getUTCDateInSeconds(t.data.dateEnd) + s.getTimeInSeconds(t.data.timeEnd);
        for (var c = t.data.prizes, p = 0; p < c.length; p++) c[p].chance = 100 * parseFloat(c[p].chance);
        h.prizes = c, h.hide_winner = t.data.lottoEnabled;
        var m = new Date().getTime();
        h.date = parseInt(m / 1e3) + 20;
    }
    wx.request({
        url: o.globalData.host + "/lotto/v1/update",
        data: h,
        method: "POST",
        success: function(i) {
            t.submitFlag = !1, s.hideLoading(), -500 != i.data.sta ? 0 == i.data.sta ? wx.navigateBack({
                delta: 1
            }) : s.showFailedToast("保存失败，请重试", i.data.msg) : s.login(function() {
                e(t, a);
            });
        },
        fail: function(e) {
            t.submitFlag = !1, s.showFailedToast("保存失败，请重试");
        }
    });
}

function a(t) {
    for (var e = 0, a = t.length, i = 0; i < a; i++) {
        if (s.isTextEmpty(t[i].name)) return 1;
        s.isTextEmpty(t[i].img) && e++;
    }
    return 0 != e ? 2 : 0;
}

function i(e) {
    s.showLoading("正在加载数据"), wx.request({
        url: o.globalData.host + "/lotto/v1/detail",
        data: {
            lid: e.data.lid,
            access_token: s.getToken()
        },
        method: "GET",
        dataType: "json",
        success: function(a) {
            if (s.hideLoading(), -500 != a.data.sta) if (0 == a.data.sta) {
                var n, o = a.data.data;
                o.date = o.date ? o.date : s.getDaysLater(3) / 1e3;
                var d = s.formatDate(1e3 * o.date, "yyyy-MM-dd HH:mm");
                o.start_date = o.start_date ? o.start_date : s.getDaysLater(0) / 1e3;
                var r = s.formatDate(1e3 * o.start_date, "yyyy-MM-dd HH:mm");
                o.end_date = o.end_date ? o.end_date : s.getDaysLater(7) / 1e3;
                var l = s.formatDate(1e3 * o.end_date, "yyyy-MM-dd HH:mm"), u = o.prizes, h = o.content;
                if ("string" == typeof h && (h = "" == h ? [] : [ {
                    type: "text",
                    value: h,
                    text: ""
                } ]), 2 == o.mode) {
                    for (var c = u, p = 0; p < c.length; p++) c[p].chance = (parseFloat(c[p].chance) / 100).toFixed(2);
                    u = c;
                }
                e.setData((n = {
                    title: o.title,
                    titleDefault: o.title,
                    mode: o.mode,
                    rule: h,
                    ruleDefault: o.content,
                    titleEmpty: 0 == o.title.length,
                    ruleEmpty: !o.content || 0 == o.content.length,
                    date: d.split(" ")[0],
                    time: d.split(" ")[1],
                    dateStart: r.split(" ")[0],
                    timeStart: r.split(" ")[1],
                    dateEnd: l.split(" ")[0],
                    timeEnd: l.split(" ")[1],
                    joinNum: o.limit,
                    oldNum: o.join_num,
                    banners: o.banners,
                    prizes: u,
                    prizesDefalut: u,
                    typeIndex: parseInt(o.typeIndex),
                    signName: o.owner_name,
                    wxNo: o.owner_wx,
                    defaultWxNo: o.owner_wx
                }, t(n, "typeIndex", o.type), t(n, "forwardIndex", o.no_forward), t(n, "token", o.req_token), 
                t(n, "tokenDesc", o.token_desc), t(n, "tokenEnabled", !!o.req_token), t(n, "qrcode", o.group_qr), 
                t(n, "lottoEnabled", o.hide_winner), n));
            } else -1 == a.data.sta && "data not found" == a.data.msg ? e.setData({
                dataNotFound: !0,
                isStartLoad: !1
            }) : (null == e.data.enroll && e.setData({
                showLoadError: 1,
                isStartLoad: !1
            }), s.showFailedToast("数据加载失败", a.data.msg)); else s.login(function() {
                i(e), e.setData({
                    hasUserInfo: !0
                });
            }, function() {
                e.setData({
                    hasUserInfo: !1
                });
            });
        },
        fail: function(t) {
            s.hideLoading(), null == e.data.enroll && e.setData({
                showLoadError: 1,
                isStartLoad: !1
            });
        }
    });
}

var s = require("../../utils/util.js"), n = require("../../tmp/tmp.js"), o = getApp();

Page({
    data: {
        lid: "",
        type: [ "按时间自动开奖", "按人数自动开奖", "手动开奖" ],
        typeIndex: 0,
        forward: [ "所有人可分享", "仅创建者可分享" ],
        forwardIndex: 0,
        showMore: !0,
        huomaDialog: !1,
        huoma: o.globalData.huoma,
        lottoType: "normal",
        mode: 0
    },
    onLoad: function(t) {
        var e = t.lid || "", a = new Date(), o = new Date();
        o.setYear(o.getFullYear() + 5), "mini" == t.type ? this.setData({
            lid: e,
            lottoType: "mini",
            minDate: s.formatDate(a, "yyyy-MM-dd"),
            maxDate: s.formatDate(o, "yyyy-MM-dd")
        }) : this.setData({
            lid: e,
            minDate: s.formatDate(a, "yyyy-MM-dd"),
            maxDate: s.formatDate(o, "yyyy-MM-dd")
        }), i(this), n.huoma(this);
    },
    onShow: function() {
        if (this.data.readCatch) {
            var t = s.getStorage("rules") || [];
            this.setData({
                rule: t
            });
        }
        this.setData({
            oldPhone: s.getStorage("userInfo").phone || ""
        });
    },
    inputTitle: function(t) {
        this.setData({
            title: t.detail.value,
            titleEmpty: 0 == t.detail.value.length
        });
    },
    clearTitle: function() {
        this.setData({
            title: "",
            titleDefault: "",
            titleEmpty: !0
        });
    },
    inputRule: function(t) {
        this.setData({
            rule: t.detail.value,
            ruleEmpty: 0 == t.detail.value.length
        });
    },
    clearRule: function() {
        this.setData({
            rule: "",
            ruleDefault: "",
            ruleEmpty: !0
        });
    },
    clearJoinNum: function() {
        this.setData({
            joinNum: "",
            joinNumEmpty: !0
        });
    },
    inputSignName: function(t) {
        this.setData({
            signName: t.detail.value
        });
    },
    clearName: function() {
        this.setData({
            signName: ""
        });
    },
    inputWxno: function(t) {
        this.setData({
            wxNo: t.detail.value.trim()
        });
    },
    clearWxno: function() {
        this.setData({
            wxNo: "",
            defaultWxNo: ""
        });
    },
    changeDate: function(t) {
        var e = t.detail.value;
        this.setData({
            date: e,
            diffTime: s.getDiffTime(e, this.data.time)
        });
    },
    changeTime: function(t) {
        var e = t.detail.value;
        this.setData({
            time: e,
            diffTime: s.getDiffTime(this.data.date, e)
        });
    },
    changeDateStart: function(t) {
        var e = t.detail.value;
        this.setData({
            dateStart: e
        });
    },
    changeTimeStart: function(t) {
        var e = t.detail.value;
        this.setData({
            timeStart: e
        });
    },
    changeDateEnd: function(t) {
        var e = t.detail.value;
        this.setData({
            dateEnd: e
        });
    },
    changeTimeEnd: function(t) {
        var e = t.detail.value;
        this.setData({
            timeEnd: e
        });
    },
    changeType: function(t) {
        this.setData({
            typeIndex: t.detail.value
        });
    },
    addPrize: function(t) {
        var e = this.data.prizes;
        2 == this.data.mode && e.length > 7 ? s.showModelTips("最多允许创建7个选项") : e.length > 9 ? s.showModelTips("最多允许创建10个选项") : (e.push({
            name: "",
            count: 1,
            img: "",
            chance: "0.00"
        }), this.setData({
            prizes: e
        }));
    },
    deletePrize: function(t) {
        var e = t.currentTarget.dataset.index;
        console.log(e);
        var a = this.data.prizes;
        a.length < 2 ? s.showModelTips("最少必须有一个奖品") : (a.splice(e, 1), this.setData({
            prizes: a
        }));
    },
    addPic: function(t) {
        var e = t.currentTarget.dataset.type, a = this, i = 1, n = 0;
        if (2 == e) {
            var o = a.data.banners;
            if (i = 6 - o.length, o.length >= 6) return void s.showModelTips("最多只能上传六张图片");
        } else n = t.currentTarget.dataset.index;
        this.setData({
            readCatch: !1
        }), wx.chooseImage({
            count: i,
            sizeType: [ "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(t) {
                var i = t.tempFilePaths;
                a.setData({
                    tempPaths: i,
                    tempIndex: 0
                }), s.showLoading("上传中..."), a.uploadImages(i, e, n);
            }
        });
    },
    uploadImages: function(t, e, a) {
        var i = t[0];
        2 == e && (i = t[a]);
        var n = this;
        wx.uploadFile({
            url: o.globalData.host + "/file/v1/upload",
            filePath: i,
            name: "file",
            formData: {
                access_token: s.getStorage("accessToken"),
                type: ""
            },
            success: function(i) {
                console.log("uploadFile ===== "), console.log(i);
                var o = i.data, d = JSON.parse(o);
                if (0 == d.sta) {
                    var r = d.data.urls;
                    if (2 == e) {
                        var l = n.data.banners;
                        l.push(r[0]), n.setData({
                            banners: l
                        }), t.length - 1 > a ? n.uploadImages(t, e, a + 1) : s.hideLoading();
                    } else if ("qrcode" == e) s.hideLoading(), n.setData({
                        qrcode: r[0]
                    }); else {
                        s.hideLoading();
                        var u = n.data.prizes;
                        u[a].img = r[0], n.setData({
                            prizes: u
                        });
                    }
                } else s.hideLoading(), s.showFailedToast(d.msg);
            },
            fail: function(t) {
                s.hideLoading(), s.showFailedToast("上传图片失败，请重试");
            }
        });
    },
    deletePic: function(t) {
        var e = t.currentTarget.dataset.type, a = t.currentTarget.dataset.index;
        if (console.log(e, a), 1 == e) {
            var i = this.data.prizes;
            console.log(i, a), i[a].img = "", this.setData({
                prizes: i
            });
        } else if ("qrcode" == e) this.setData({
            qrcode: ""
        }); else {
            var s = this.data.banners;
            s.splice(a, 1), this.setData({
                banners: s
            });
        }
    },
    showPic: function(t) {
        var e = t.currentTarget.dataset.type, a = t.currentTarget.dataset.url, i = [ a ];
        2 == e && (i = this.data.banners), wx.previewImage({
            current: a,
            urls: i
        });
    },
    huomaDialog: function() {
        this.setData({
            huomaDialog: !0
        });
    },
    inputName: function(t) {
        var e = t.currentTarget.dataset.index, a = this.data.prizes;
        a[e].name = t.detail.value, this.setData({
            prizes: a
        });
    },
    inputCount: function(t) {
        var e = t.currentTarget.dataset.index;
        this.setPrizesCount(e, t.detail.value);
    },
    inputBlur: function(t) {
        var e = t.currentTarget.dataset.index, a = t.detail.value;
        a = "" == a ? 0 : a, this.setPrizesCount(e, a);
    },
    inputChance: function(t) {
        var e = t.currentTarget.dataset.index;
        this.setPrizesChance(e, t.detail.value);
    },
    inputBlurChance: function(t) {
        var e = t.currentTarget.dataset.index, a = t.detail.value;
        a = "" == a ? 0 : a, this.setPrizesChance(e, a);
    },
    setPrizesChance: function(t, e) {
        if (e >= 100 || e < 0) return this.setData({
            prizes: this.data.prizes
        }), void s.showModelTips("请输入正确的中奖概率");
        (a = this.data.prizes)[t].chance;
        return void (a[t].chance = parseFloat(e));
        var a;
    },
    setPrizesCount: function(t, e) {
        var a = this.data.prizes;
        a[t].count = parseInt(e), a[t].count < 1 && (a[t].count = 1), this.setData({
            prizes: a
        });
    },
    minusPlus: function(t) {
        var e = t.currentTarget.dataset.index, a = t.currentTarget.dataset.type, i = 1;
        console.log(e), console.log(a), "minus" == a && (i = -1);
        var s = this.data.prizes;
        console.log(s), s[e].count = s[e].count + i, s[e].count > 999 && (s[e].count = 999), 
        s[e].count < 1 && (s[e].count = 1), this.setData({
            prizes: s
        });
    },
    previewPage: function() {
        s.setStorage("lotto_preview", this.data), wx.navigateTo({
            url: "/pages/preview/preview"
        });
    },
    touchstart: function(t) {
        var e = t.currentTarget.dataset.index, a = t.currentTarget.dataset.type, i = this.data.prizes;
        "minus" == a ? (i[e].pressMinus = !0, this.setData({
            prizes: i
        })) : (i[e].pressPlus = !0, this.setData({
            prizes: i
        }));
    },
    touchend: function() {
        for (var t = this.data.prizes, e = 0; e < t.length; e++) t[e].pressMinus = !1, t[e].pressPlus = !1;
        this.setData({
            prizes: t
        });
    },
    joinNum: function(t) {
        this.setData({
            joinNum: t.detail.value,
            joinNumEmpty: !t.detail.value.length
        });
    },
    submitFlag: !1,
    submitLotto: function(t) {
        if (console.log("========edit=============="), console.log(this.data), !this.submitFlag) {
            if (this.submitFlag = !0, setTimeout(function() {
                this.submitFlag = !1;
            }, 5e3), s.isTextEmpty(this.data.title)) return s.showModelTips("请输入抽奖活动标题"), void (this.submitFlag = !1);
            var i = s.getUTCDateInSeconds(this.data.dateStart) + s.getTimeInSeconds(this.data.timeStart), n = s.getUTCDateInSeconds(this.data.dateEnd) + s.getTimeInSeconds(this.data.timeEnd), o = new Date().getTime();
            if (2 == this.data.mode) {
                if (i >= n) return s.showModelTips("抽奖开始时间不能大于结束时间"), void (this.submitFlag = !1);
                if (n <= parseInt(o / 1e3)) return s.showModelTips("结束时间不能小于当前时间"), void (this.submitFlag = !1);
                for (var d = !1, r = 0, l = this.data.prizes, i = 0; i < l.length; i++) 0 == parseFloat(l[i].chance) && (d = !0), 
                r += 100 * parseFloat(l[i].chance);
                if (d) return s.showModelTips("奖项中奖概率不能为0"), void (this.submitFlag = !1);
                if (r > 1e4) return s.showModelTips("所有奖品中奖概率总和不能大于100"), void (this.submitFlag = !1);
            }
            if (1 == a(this.data.prizes)) return s.showModelTips("请将奖项标题填写完整"), void (this.submitFlag = !1);
            if (2 == a(this.data.prizes)) return s.showModelTips("您有未上传的奖项图片，请检查~"), void (this.submitFlag = !1);
            if (this.data.prizes.length < 1) return s.showModelTips("至少需要一个奖项"), void (this.submitFlag = !1);
            var u = this.data.joinNum;
            if ((0 == u || "" == u) && 1 == this.data.typeIndex) return s.showModelTips("请输入开奖人数"), 
            void (this.submitFlag = !1);
            if (u <= this.data.oldNum && 1 == this.data.typeIndex) return s.showModelTips("开奖人数不能小于已参与人数"), 
            void (this.submitFlag = !1);
            if (0 == this.data.typeIndex && 2 != this.data.mode && new Date().getTime() >= 1e3 * (s.getUTCDateInSeconds(this.data.date) + s.getTimeInSeconds(this.data.time))) return s.showModelTips("开奖时间必须大于当前时间"), 
            void (this.submitFlag = !1);
            "preview" == t.currentTarget.dataset.type ? this.previewPage() : (wx.setStorageSync("create_lotto", 1), 
            s.removeStorage("rules"), e(this, t));
        }
    },
    showMore: function() {
        var t = this.data.showMore;
        this.setData({
            showMore: !t
        });
    },
    inputToken: function(t) {
        this.setData({
            tokenEmpty: 0 == t.detail.value.length
        });
    },
    clearToken: function() {
        this.setData({
            token: "",
            tokenEmpty: !0
        });
    },
    inputTokenDesc: function(t) {
        this.setData({
            tokenDescEmpty: 0 == t.detail.value.length
        });
    },
    clearTokenDesc: function() {
        this.setData({
            tokenDesc: "",
            tokenDescEmpty: !0
        });
    },
    changeTokenEnabled: function(t) {
        this.setData({
            tokenEnabled: t.detail.value,
            token: "",
            tokenDesc: ""
        });
    },
    changeForward: function(t) {
        this.setData({
            forwardIndex: t.detail.value
        });
    },
    changeLottoEnabled: function(t) {
        this.setData({
            lottoEnabled: t.detail.value ? 0 : 1
        });
    },
    toRules: function() {
        s.buttonClicked(this), s.setStorage("rules", this.data.rule), this.setData({
            readCatch: !0
        }), wx.navigateTo({
            url: "/pages/rules/rules"
        });
    }
});